package com.brkj.download;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.View.OnClickListener;

public class DownLoadCourseManager {
	private Context context;

	public final static int MAX_DOWNLOAD_COURSE_NUM = 20;
	
	private List<String> exceptFileList = new ArrayList<String>();
	
	public DownLoadCourseManager(Context context) {
		this.context = context;
	}
	
	
	public void DeleteAllCourse(){
//		final SchoDialog dlg = new SchoDialog(context, "清空所有已下载课程？");
//		dlg.SetListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				
//				dlg.dismiss();
//				
//				DBDownloadCourseHelper dbDownload = new DBDownloadCourseHelper(
//						context);
//				SQLiteDatabase db_download = dbDownload.getWritableDatabase();
//
//				db_download.execSQL("delete from "
//						+ DBDownloadCourseHelper.DOWNLOAD_COURSE_TABLE_NAME 
//						+ " where " + DBDownloadCourseHelper.ISDOWNLOADFINISHED
//						+ " = " + "100");
//				db_download.close();
//				
//				FileCache fileCache = new FileCache(context);
//				if(delAllFile(fileCache.GetVideoFileDir())){
//					ToastShow.showToast(context, "已清空！");
//				}else{
//					ToastShow.showToast(context, "删除视频文件遇到问题！");
//				}
//				
//				Intent intent = new Intent();
//				intent.setAction("com.brkj.DeleteAllCourseFinish");
//				context.sendBroadcast(intent);
//			}
//		});
		 
//		dlg.show();
	}
	
	private boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				boolean noDelete = false;
				for(int k=0; k<exceptFileList.size(); k++){
					if(temp.getName().equals(exceptFileList.get(k))){
						noDelete = true;
						break;
					}
				}
				
				if(!noDelete){
					temp.delete();
				}
			}
		}
		flag =true;
		
		return flag;
	}

	public void setExceptFile(List<String> downloadingtFileList) {
		exceptFileList = downloadingtFileList;
	}
}
